//
//  BaseTableViewCell.m
//  XDLBaseProject
//
//  Created by Xiedonglai on 17/1/12.
//  Copyright © 2017年 Apple. All rights reserved.
//

#import "BaseTableViewCell.h"

@interface BaseTableViewCell()

@property (strong, nonatomic) UIView *topLine;
@property (strong, nonatomic) UIView *bottomLine;

@end

@implementation BaseTableViewCell

+ (instancetype)cellWithTableView:(UITableView *)tableView indentifier:(NSString *)indentifier style:(UITableViewCellStyle)cellStyle {
    BaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifier];
    if (!cell) {
        cell = [[self alloc] initWithStyle:cellStyle reuseIdentifier:indentifier];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    [self internalInit];
    return self;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    [self internalInit];
}

- (void)internalInit {
    self.separateLineOffset = 30;
    
    _topLine = [[UIView alloc] init];
    _topLine.backgroundColor = [AppColor getColor_DIVIDING];
    _bottomLine = [[UIView alloc] init];
    _bottomLine.backgroundColor = [AppColor getColor_DIVIDING];
    //默认的两条边线是隐藏的
    _topLine.hidden = YES;
    _bottomLine.hidden = YES;
    
    [self addSubview:_topLine];
    [self addSubview:_bottomLine];
}

- (void)setCellStyle:(BaseCellStyle)cellStyle {
    _cellStyle = cellStyle;
    switch (cellStyle) {
        case BaseCellStyleFirst:
            _topLine.hidden = NO;
            _bottomLine.hidden = NO;
            break;
        case BaseCellStyleMiddle:
            _topLine.hidden = YES;
            _bottomLine.hidden = NO;
            break;
        case BaseCellStyleLast:
            _topLine.hidden = YES;
            _bottomLine.hidden = NO;
            break;
        case BaseCellStyleTop:
            _topLine.hidden = NO;
            _bottomLine.hidden = YES;
            break;
        case BaseCellStyleBottom:
            _topLine.hidden = YES;
            _bottomLine.hidden = NO;
            break;
        case BaseCellStyleTopAndBottom:
            _topLine.hidden = NO;
            _bottomLine.hidden = NO;
            break;
        case BaseCellStyleNone:
            _topLine.hidden = YES;
            _bottomLine.hidden = YES;
            break;
        default:
            break;
    }
    [self setNeedsLayout];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    switch (_cellStyle) {
        case BaseCellStyleFirst:
            _topLine.frame = CGRectMake(0, 0, kScreenW, DIVIDING_LINE);
            _bottomLine.frame = CGRectMake(_separateLineOffset, self.frame.size.height - DIVIDING_LINE, kScreenW - _separateLineOffset, DIVIDING_LINE);
            break;
        case BaseCellStyleMiddle:
            _topLine.frame = CGRectMake(_separateLineOffset, 0, kScreenW - _separateLineOffset, DIVIDING_LINE);
            _bottomLine.frame = CGRectMake(_separateLineOffset, self.frame.size.height - DIVIDING_LINE, kScreenW - _separateLineOffset, DIVIDING_LINE);
            break;
        case BaseCellStyleLast:
            _topLine.frame = CGRectMake(_separateLineOffset, 0, kScreenW - _separateLineOffset, DIVIDING_LINE);
            _bottomLine.frame = CGRectMake(0, self.frame.size.height - DIVIDING_LINE, kScreenW - _separateLineOffset, DIVIDING_LINE);
            break;
        case BaseCellStyleTop:
            _topLine.frame = CGRectMake(0, 0, kScreenW, DIVIDING_LINE);
            _bottomLine.frame = CGRectMake(_separateLineOffset, self.frame.size.height - DIVIDING_LINE, kScreenW - _separateLineOffset, DIVIDING_LINE);
            break;
        case BaseCellStyleBottom:
            _topLine.frame = CGRectMake(0, 0, kScreenW, DIVIDING_LINE);
            _bottomLine.frame = CGRectMake(0, self.frame.size.height - DIVIDING_LINE, kScreenW, DIVIDING_LINE);
            break;
        case BaseCellStyleTopAndBottom:
            _topLine.frame = CGRectMake(0, 0, kScreenW, DIVIDING_LINE);
            _bottomLine.frame = CGRectMake(0, self.frame.size.height - DIVIDING_LINE, kScreenW, DIVIDING_LINE);
            break;
        default:
            break;
    }
}

- (void)setSeperatorLine:(NSIndexPath *)indexPath numberOfRowsInSection:(NSInteger)numberOfRowsInSection offset:(CGFloat)separateLineOffset {
    self.separateLineOffset = separateLineOffset;
    if (indexPath.row == 0 && numberOfRowsInSection == 1) {
        self.cellStyle = BaseCellStyleTopAndBottom;
    } else if (indexPath.row == 0 && numberOfRowsInSection > 1) {
        self.cellStyle = BaseCellStyleFirst;
    } else if (indexPath.row == numberOfRowsInSection - 1 && numberOfRowsInSection > 1) {
        self.cellStyle = BaseCellStyleLast;
    } else if (indexPath.row > 0 && indexPath.row < numberOfRowsInSection - 1 && numberOfRowsInSection > 1) {
        self.cellStyle = BaseCellStyleMiddle;
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

@end
